package com.itheima.wms.service.impl;

import com.itheima.wms.mapper.NextCodeMapper;
import com.itheima.wms.pojo.CodeFactory;
import com.itheima.wms.service.NextCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@RequiredArgsConstructor
@Service
@Transactional
public class NextCodeServiceImpl implements NextCodeService {
    private final NextCodeMapper nextCodeMapper;

    @Override
    public String NextCode(String type) {
        Object code = nextCodeMapper.queryNextCode(type);
        //之前没有 需要加入
        if (code == null) {
            code = 1001;
            CodeFactory codeFactory = new CodeFactory(null, type, code + "", 1, type, LocalDate.now(), LocalDate.now());
            nextCodeMapper.InsertNextCode(codeFactory);
        } else {
            //之前有 更新数据
            int c = Integer.parseInt(code.toString());
            c += 1;
            nextCodeMapper.updateNextCode(type, c + "", LocalDate.now());
        }
        return type + code;
    }
}
